package homework;

/**
 * 4）、模拟一个银行存款的程序。假设有两个储户都去银行往同一个账户进行存款，
 * 一次存100，每人存三次。要求储户每存一次钱，账户余额增加100，并在控制台输出当前账户的余额。
 */




class Bank {
    private static int price;
    public synchronized void saveMoney(int money){
        price += money;
        System.out.println("当前余额为："+price);
    }
}

class SaveMoney implements Runnable{
    @Override
    public void run() {
        Bank bank = new Bank();
        synchronized (this){
            for (int i = 1; i <= 3; i++) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"第"+i+
                        "次，存款100元。");
                bank.saveMoney(100);
            }

        }
    }
}

public class No_4 {


    public static void main(String[] args) {


        new Thread(new SaveMoney(),"用户 1 ").start();
        new Thread(new SaveMoney(),"用户 2 ").start();


    }
}
